/**
 * 将tree转化为组件对应的格式
 * @param arr
 * @param oldKey1
 * @param newKey1
 * @param oldKey2
 * @param newKey2
 */
export function changeTree(arr, oldKey1, newKey1, oldKey2, newKey2) {
  arr.forEach((item) => {
    if (item[oldKey1]) {
      item[newKey1] = item[oldKey1]
      delete item[oldKey1]
    }
    if (item[oldKey2]) {
      item[newKey2] = item[oldKey2]
      delete item[oldKey2]
    }
    if (item.children) {
      changeTree(item.children, oldKey1, newKey1, oldKey2, newKey2)
    }
  })
}

/**
 * 生成菜单树
 * @param items
 * @param parentId
 */
export function generateTree(items, parentId = 0) {
  const tree = []

  items.forEach((item) => {
    if (item.parentId === parentId) {
      const children = generateTree(items, item.menuId)
      if (children.length) {
        item.children = children
      }
      tree.push(item)
    }
  })

  return tree
}

/**
 * 递归子树找到子级
 * @param items
 */
export function treeChildren(items) {
  const ids = []

  function recurseGetId(menus) {
    for (const menu of menus) {
      if (menu.children) {
        recurseGetId(menu.children)
      } else {
        ids.push(menu.menuId)
      }
    }
  }

  recurseGetId(items)
  return ids
}
